package phidgetmotion.gui.util;

import javax.swing.text.Document;

public abstract class IntegerDataSource implements TextDataSource
{
	private boolean canBeNull;
	
	public IntegerDataSource(boolean canBeNull)
	{
		this.canBeNull = canBeNull;
	}
	
	@Override
	public String get()
	{
		return String.valueOf(getData());
	}

	@Override
	public Document getDocument()
	{
		return new IntegerDocument();
	}

	@Override
	public void set(String data)
	{
		if(data.equals("")) return;
		int intData =Integer.parseInt(data);
		if(canBeNull || intData>0) setData(intData);
	}
	
	protected abstract int getData();
	protected abstract void setData(int data);

}
